/*
 给出如下地址数据格式，实现函数 getNameById ，输⼊address 和 id ，输出 id 对应的地址 name 
 */
const address = [{
    id: 1,
    name: '北京市',
    children: [{
        id: 11,
        name: '海淀区',
        children: [{
            id: 111,
            name: '中关村'
        }]
    }, {
        id: 12,
        name: '朝阳区'
    }],
}, {
    id: 2,
    name: '河北省'
}];

const getNameById = (address, id) => {
    // 这里实现你的代码
    if (!Array.isArray(address)) return "";
    let result = "";
    const next = arr => {
        for (let i = 0; i < arr.length; i++) {
            let item = arr[i];
            if (+item.id === +id) {
                result = item.name;
                break;
            }
            if (Array.isArray(item.children)) next(item.children);
        }
    };
    next(address);
    return result;
};
console.log(getNameById(address, 2)); //输出："河北省"
console.log(getNameById(address, 111)); //输出："中关村"
console.log(getNameById(address, 32)); //输出：""